package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.controller.vo.ProductAddVo;
import com.example.controller.vo.ProductUpdateVo;
import com.example.controller.vo.Result;
import com.example.entity.PageView;
import com.example.entity.Product;
import com.example.entity.User;
import com.example.service.IInteractionService;
import com.example.service.IProductService;
import com.example.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author yzy
 * @since 2025-08-04
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IInteractionService iInteractionService;
    @Autowired
    private IUserService iuserService;

    /**
     * 查询所有商品信息
     */
    //包括分页以及模糊查询根据是否支持砍价 || 商品类别 || 商品名
    @GetMapping("/list")
    public Result getProductList(@RequestParam(value = "pageNum",defaultValue = "1",required = false) int pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "5",required = false) int pageSize,
                                 @RequestParam(value = "isBargain",required = false) Integer isBargain,
                                 @RequestParam(value = "categoryId",required = false) Integer categoryId,
                                 @RequestParam(value = "name",required = false) String name){
        Page<Product>productPage = productService.pagelist(pageNum,pageSize,isBargain,categoryId,name);

        return Result.success(productPage);
    }
    /** 按id查询商品信息 */
    @GetMapping("/admin/{id}")
    public Result getProductByid(@PathVariable("id") Integer id,
                                 @RequestParam(required = false) String userName){
        //管理员访问不提供userName
        if (userName == null || userName.trim().isEmpty()){
        Product product = productService.getById(id);
        return Result.success(product);
        }else {
            //用户查看商品信息
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("user_name",userName);
            User user = iuserService.getOne(queryWrapper);

            Integer userId=user.getId();
            Result result = productService.userGetById(id,userId);
            return result;

        }

    }

    /**
     * 按id删除商品
     */
    @DeleteMapping("/{id}")
    public Result deleteProduct(@PathVariable Integer id) {
        Result result = productService.deleteProduct(id);
        return result;
    }
    /**我的商品页面*/
    @GetMapping("/userlist")
    public Result getUserProductList(@RequestParam(value = "pageNum",defaultValue = "1",required = false) int pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "5",required = false) int pageSize,
                                 @RequestParam(value = "userName") String userName
                                 ){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account",userName);
        User user = iuserService.getOne(queryWrapper);
        Integer userId=user.getId();
        Page<Product>productPage = productService.pageUserlist(pageNum,pageSize,userId);
        PageView pageView = new PageView();
        pageView.setPageNum(Integer.parseInt(productPage.getCurrent()+""));
        pageView.setPageSize(Integer.parseInt(productPage.getSize()+""));
        pageView.setAllRow(productPage.getTotal());
        pageView.setTotalPage(productPage.getPages());
        pageView.setData(productPage.getRecords());
        return Result.success(pageView);
    }
    /**我的收藏,我的足迹*/
    @GetMapping("/collect")
    public Result getUserProductcollect(@RequestParam(value = "pageNum",defaultValue = "1",required = false) int pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "5",required = false) int pageSize,
                                     @RequestParam(value = "userName") String userName,
                                     @RequestParam(value = "type") Integer type
    ){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account",userName);
        User user = iuserService.getOne(queryWrapper);
        Integer userId=user.getId();
        Page<Product>productPage = productService.pageUsercollect(pageNum,pageSize,userId,type);
        if (productPage != null&&!"".equals(productPage)) {
        PageView pageView = new PageView();
        pageView.setPageNum(Integer.parseInt(productPage.getCurrent()+""));
        pageView.setPageSize(Integer.parseInt(productPage.getSize()+""));
        pageView.setAllRow(productPage.getTotal());
        pageView.setTotalPage(productPage.getPages());
        pageView.setData(productPage.getRecords());
        return Result.success(pageView);
        }else if(type==1){
            return Result.failed("用户没有收藏");}
        else {
            return Result.failed("用户没有足迹");
        }

    }
    /**新增商品*/
    @PostMapping("/add")
    public Result addproduct(@RequestBody ProductAddVo productAddVo){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account", productAddVo.getUserName());
         User user = iuserService.getOne(queryWrapper);
        Integer userId=user.getId();
        productService.addProduct(productAddVo,userId);
        return Result.success("商品添加成功");
    }
    @PostMapping("/update")
    public Result updateProduct(@RequestBody ProductUpdateVo updateVo) {
        // 校验商品ID
        if (updateVo.getId() == null) {
            return Result.failed("商品ID不能为空");
        }
        // 调用服务层修改方法
        productService.updateProduct(updateVo);
        return Result.success("商品修改成功");
    }

}
